package cn.foolishbird.hamal.frame.base;

import cn.foolishbird.hamal.frame.exception.base.BaseException;
import cn.foolishbird.hamal.frame.queryParam.QueryCondition;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Eric
 * @version 1.0
 * @date 2020-03-11
 */
public class CustomBaseController<T, S extends CustomService<T>> {

    @Autowired
    protected S service;

    /**
     * id 转换成 对应后台管理显示身份信息
     *
     * @param id
     * @return cn.foolishbird.hamal.frame.base.Rst<java.lang.String>
     * @throws
     * @author Eric
     * @date 2020/10/15
     **/
    @GetMapping(value = "/joinConvert/{id}")
    protected Rst<String> joinConvert(
            @ApiParam(name = "id", value = "字典id", required = true) @PathVariable(name = "id") Long id
    ) throws BaseException {
        return RstFactory.success(service.joinConvert(id));
    }

    /**
     * 详情
     *
     * @param id
     * @return cn.foolishbird.hamal.frame.base.Rst<T>
     * @throws
     * @author Eric
     * @date 2020/10/15
     **/
    @GetMapping(value = "/detail/{id}")
    protected Rst<T> detail(
            @ApiParam(name = "id", value = "字典id", required = true) @PathVariable(name = "id") Long id
    ) throws BaseException {
        return RstFactory.success(service.managerDetail(id));
    }

    /**
     * 添加
     *
     * @param data
     * @return cn.foolishbird.hamal.frame.base.Rst<T>
     * @throws
     * @author Eric
     * @date 2020/10/15
     **/
    @PostMapping(value = "/add")
    protected Rst<T> add(
            @ApiParam(value = "字典信息", required = true) @RequestBody T data
    ) throws BaseException {
        return RstFactory.success(service.managerAdd(data));
    }

    /**
     * 更新
     *
     * @param data
     * @return cn.foolishbird.hamal.frame.base.Rst<T>
     * @throws
     * @author Eric
     * @date 2020/10/15
     **/
    @PostMapping(value = "/update")
    protected Rst<T> update(
            @ApiParam(value = "信息", required = true) @RequestBody T data
    ) throws BaseException {
        return RstFactory.success(service.managerUpdate(data));
    }

    /**
     * 删除
     *
     * @param ids
     * @return cn.foolishbird.hamal.frame.base.Rst<java.lang.Boolean>
     * @throws
     * @author Eric
     * @date 2020/10/15
     **/
    @PostMapping(value = "/delete")
    public Rst<Boolean> delete(
            @ApiParam(name = "ids", value = "表Id", required = true) @RequestParam(name = "ids") Long[] ids
    ) throws BaseException {
        return RstFactory.success(service.managerDelete(ids));
    }

    /**
     * 分页查询
     *
     * @param condition
     * @return cn.foolishbird.hamal.frame.base.Rst<com.baomidou.mybatisplus.core.metadata.IPage < T>>
     * @throws
     * @author Eric
     * @date 2020/10/15
     **/
    @PostMapping(value = "/page")
    public Rst<IPage<T>> page(
            @ApiParam(name = "condition", value = "查询条件", required = true) @RequestBody QueryCondition condition
    ) throws BaseException {
        return RstFactory.success(service.managerPage(condition));
    }

}
